<?php
/*
* Kusht Mud.
* ----
* The MIT License
* Copyright (c) 2010 Andrew Tutt
* ----
*/

/* PHP Utilities
*
* PHP Lacks some important functions. They are added here.
*/

// Delete an element from an array
function array_del_elem(&$sarray,$element) {
	// Not the fastest form. But it is error-free across systems
	foreach($sarray as $k=>$e) {
		if($e === $element) { unset($sarray[$k]); return; }
	}
	return false;
}

// Delete an element from an array by a field match
function array_del_elem_by_stristr(&$sarray,$element,$field) {
	// Not the fastest form. But it is error-free across systems
	foreach($sarray as $k=>$e) {
		if(stristr($e->$field,$element->$field)) { unset($sarray[$k]); return; }
	}
	return false;
}

// Set the Fields of Two Different Objects with the same fields equal
// $obj1 is target for assignment, $obj2 is source (preserves class definitions)
function set_fields_equal($obj1,$obj2) {
	foreach($obj2 as $key => $field) {
		$obj1->$key = $field;
	}
}

// Take all of the string fields of an object and make them safe to go into a database
function add_slashes_object($obj) {
	rmv_slashes_object($obj); // In case this has been called before, we don't want duplication
	foreach($obj as $key=>$field) {
		if(is_string($field)) { $obj->$key = addslashes($field); }
	}
	return $obj;
}

// Take all of the string fields of an object and undo "addslashes" if necessary
function rmv_slashes_object($obj) {
	foreach($obj as $key=>$field) {
		if(is_string($field)) { 
			$obj->$key = $field; 
			$obj->$key = str_replace("\'","'",$obj->$key); 
			$obj->$key = str_replace('\"','"',$obj->$key); 
			$obj->$key = str_replace("\\\\","\\",$obj->$key); 
		}
	}
	return $obj;
}

function rmv_outer_quotes($str) {
	if($str[0] == '"' && $str[strlen($str)-1] == '"') {
		return substr($str,1,strlen($str)-2);
	}
	if($str[0] == "'" && $str[strlen($str)-1] == "'") {
		return substr($str,1,strlen($str)-2);
	}
	return $str;
}

// Get an object from an Array by the value of one of it's fields
function get_obj_by_field_value($value,$field,$array) {
	if(!is_array($array)) return false;// Filter out bad calls
	foreach($array as $element) {
		if($element->$field==$value) { return $element; } // We have to use ==
	}
	return false;
}

// A loose search using stristr (only available on string fields)
function get_obj_by_stristr($value,$field,$array) {
	if(!is_array($array)) { return false; } // Filter out bad calls
	if($value == "") { return false; }
	foreach($array as $element) {
		if(stristr($element->$field,$value)) { return $element; } // We have to use ==
	}
	return false;
}

// Given bounds $a and $c, if $b > $c return $c and if $b < $a return $a, else return $b
// Allows us to bound any integer between two limits
function bound_between_limits ($a, $b, $c) { 
	return (($b) < ($a) ? ($a) : (($b) > ($c) ? ($c) : ($b))); 
}

/* --- MORE EFFICIENT VERSIONS OF THE BIT FUNCTIONS --- */
/* NOTE: These Take a Number as a numerical index, and are thus
*  Customized to this specific application and not really a 
*  PHP Utility: Oh well.
*/

function bit_index($num) {
	return floor($num / 31);
}

function to_bit($num) {
	return 1 << $num-(bit_index($num))*31;
}

function SET_EXTENDED_BIT(&$var, $num) {
	$i = bit_index($num);
	if(!isset($var[$i])) { $var[$i]=0; }
	$var[$i] = ($var[$i] | to_bit($num));
}

function RMV_EXTENDED_BIT(&$var, $num)	{
	$i = bit_index($num);
	if(!isset($var[$i])) { $var[$i]=0; }
	$var[$i] = ($var[$i] & ~to_bit($num));
}

function IS_SET_EXTENDED($flag, $num) {
	$i = bit_index($num);
	if(!isset($flag[$i])) { return false; }
	return ($flag[$i] & to_bit($num))>0;
}

/* ---- Our random numbers ----- */

// Uniform draw on [0,1]
function uRand() {
	return (rand() / getrandmax());
}

// Draw from a Normal Distribution
function nrmRand() {
	$pi = 3.141592653589793;
	$i = uRand();
	$j = uRand();
	return sqrt(-2*log($i))*cos(2*$pi*$j);
}

// Draw from the Positive Tail of a Normal Distribution
function nrmRandAbs() {
	return abs(nrmRand());
}


?>